/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.table;

import java.awt.Color;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;

import org.jxSwing.Cleanable;
import org.jxSwing.component.JXComponent;
import org.jxSwing.i18n.I18N;
import org.jxUtils.TypeUtil;
import org.jxUtils.check.CheckUtil;
import org.jxUtils.check.ParameterCheck;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.table
 * @date 19/09/2014 - 20:01:50
 */
public class ResultSetTable extends JTable implements JXComponent, Cleanable {
	
	private static final long serialVersionUID = 5408131425464395376L;
	
	private final List<String> columns = new ArrayList<>();
	
	private final List<Class<?>> columnClasses = new ArrayList<>();
	
	private boolean atributte = true;
	
	private boolean required = false;
	
	private boolean requiredBorderEnabled = true;
	
	private int index = -1;
	
	/**
	 * 
	 */
	public ResultSetTable() {
	
		initGUI();
	}
	
	/**
	 * @param resultSet
	 * @throws SQLException
	 */
	public ResultSetTable(final ResultSet resultSet) throws SQLException {
	
		ParameterCheck.isNull(resultSet, "resultSet");
		
		initGUI();
		
		setResultSet(resultSet);
	}
	
	/**
	 *
	 */
	private void initGUI() {
	
		setAutoResizeMode(AUTO_RESIZE_OFF);
		setRowHeight(25);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.Cleanable#clear()
	 */
	@Override
	public final void clear() {
	
		//
		while(getModel().getRowCount() > 0) {
			
			getModel().removeRow(0);
		}
		
		//
		getModel().fireTableDataChanged();
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#getIndex()
	 */
	@Override
	public final int getIndex() {
	
		return index;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JTable#getModel()
	 */
	@Override
	public final DefaultTableModel getModel() {
	
		return (DefaultTableModel)super.getModel();
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#isAtributte()
	 */
	@Override
	public final boolean isAtributte() {
	
		return atributte;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#isRequired()
	 */
	@Override
	public final boolean isRequired() {
	
		return required;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#isRequiredBorderEnabled()
	 */
	@Override
	public final boolean isRequiredBorderEnabled() {
	
		return requiredBorderEnabled;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setAtributte(boolean)
	 */
	@Override
	public final void setAtributte(final boolean atributte) {
	
		this.atributte = atributte;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setIndex(int)
	 */
	@Override
	public final void setIndex(final int index) {
	
		this.index = index;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setRequired(boolean)
	 */
	@Override
	public final void setRequired(final boolean required) {
	
		this.required = required;
		
		//
		if(required) {
			
			//
			if(isRequiredBorderEnabled()) {
				
				setBorder(new LineBorder(Color.RED));
			}
			
			//
			if(CheckUtil.isNullOrAbsoluteEmpty(getName())) {
				
				setToolTipText(I18N.component().requiredThis());
				
			}else {
				
				setToolTipText(I18N.component().requiredField(getName()));
			}
			
		}else {
			
			setBorder(UIManager.getBorder("Table.scrollPaneBorder"));
			setToolTipText(null);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setRequiredBorderEnabled(boolean)
	 */
	@Override
	public final void setRequiredBorderEnabled(final boolean requiredBorderEnabled) {
	
		this.requiredBorderEnabled = requiredBorderEnabled;
	}
	
	/**
	 * @param resultSet
	 * @throws SQLException
	 */
	public final void setResultSet(final ResultSet resultSet) throws SQLException {
	
		ParameterCheck.isNull(resultSet, "resultSet");
		
		columns.clear();
		columnClasses.clear();
		
		ResultSetMetaData metaData = null;
		
		//
		if(resultSet.next()) {
			
			metaData = resultSet.getMetaData();
			
			//
			for(int column = 1; column <= metaData.getColumnCount(); column++) {
				
				columns.add(metaData.getColumnName(column));
				
				columnClasses.add(TypeUtil.sqlTypeToJavaType(metaData.getColumnType(column)));
			}
		}
		
		//
		super.setModel(new DefaultTableModel(new Object[][]{}, columns.toArray()) {
			
			private static final long serialVersionUID = -2691706716178703582L;
			
			@Override
			public Class<?> getColumnClass(final int columnIndex) {
			
				return columnClasses.get(columnIndex);
			}
			
			@Override
			public boolean isCellEditable(final int row, final int column) {
			
				return false;
			}
		});
		
		// 
		if(metaData != null) {
			
			do {
				//
				final Vector<Object> rowData = new Vector<>();
				
				//
				for(int columnIndex = 1; columnIndex <= metaData.getColumnCount(); columnIndex++) {
					
					rowData.add(resultSet.getObject(columnIndex));
				}
				
				//
				getModel().addRow(rowData);
				
			}while(resultSet.next());
		}
	}
}
